/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：OrderLogDaoImpl.java
 * History:
 *       Apr 16, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IOrderLogDao;
import com.xteam.ecsystem.entities.BaseOrderInfo;
import com.xteam.ecsystem.entities.LogOrderLog;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 订单日志信息数据访问实现类
 */
@Repository
@SuppressWarnings("unchecked")
public class OrderLogDaoImpl extends DDLPrefix implements IOrderLogDao {

	@Autowired
	private BaseDaoImpl baseDao;
	
	public String getLolListByOrderId(String orderId){
		JSONArray ja = new JSONArray();
		try {
			List list = new ArrayList();
			
			String hql = SQL_FROM + LogOrderLog.REF + SQL_WHERE + "lolOrder=?" 
					+ SQL_ORDERBY + "lolTime desc";
			list = this.baseDao.find(hql, new Object[]{orderId});
			
			if (!list.isEmpty()) {
				ja = JSONArray.fromObject(list);
				return ja.toString();
			}
			

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "";
	}
	
	public void saveOrderLog(String lolOtype, String lolOrder, String lolUtype,
			String lolUser, String lolAddress, Date lolTime, String lolOremark){
		try {
			LogOrderLog orderLog = new LogOrderLog();
			orderLog.setLolOtype(lolOtype);
			orderLog.setLolOrder(lolOrder);
			orderLog.setLolUtype(lolUtype);
			orderLog.setLolUser(lolUser);
			orderLog.setLolAddress(lolAddress);
			orderLog.setLolTime(lolTime);
			orderLog.setLolOremark(lolOremark);
			
			this.baseDao.save(orderLog);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getOrderLogList(String boiOsn, String lolUser, int pageIndex, int pageSize){
		JSONObject jo = new JSONObject();
		List list = new ArrayList();
		
		try {
			String hql = SQL_SELECT + "new " + LogOrderLog.REF + " (lol.lolId, lol.lolOtype, lol.lolOrder, "
					+ "lol.lolUtype, lol.lolUser, lol.lolAddress, lol.lolTime, lol.lolOremark, boi.boiOsn)"
					+ SQL_FROM + LogOrderLog.REF + " as lol," + BaseOrderInfo.REF + " as boi" 
					+ SQL_WHERE + "lol.lolOrder = boi.boiId";
			
			String countHQL = SQL_SELECT + "count(*)" + SQL_FROM + LogOrderLog.REF + " as lol," 
					+ BaseOrderInfo.REF + " as boi" + SQL_WHERE + "lol.lolOrder = boi.boiId";
			
			if (StringUtil.isNotNullOrEmpty(boiOsn)) {
				hql += SQL_AND + "boi.boiOsn=?";
				countHQL += SQL_AND + "boi.boiOsn=?";
				list.add(boiOsn);
			}
			
			if (StringUtil.isNotNullOrEmpty(lolUser)) {
				hql += SQL_AND + "lol.lolUser like ?";
				countHQL += SQL_AND + "lol.lolUser like ?";
				list.add("%" + lolUser + "%");
			}
			
			hql += SQL_ORDERBY + "lol.lolTime desc";

			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao.queryList(countHQL, hql, list
					.toArray(), pageIndex * pageSize, pageSize);
			
			jo = JSONObject.fromObject(p);
			return jo.toString();

		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		
	}
	
	public void delOrderLogsByIds(String ids){
		try {
			List list = new ArrayList();
			String oId[] = ids.split(",");
			for (String id : oId) {
				Object obj = this.baseDao.get(LogOrderLog.class, id);
				if (obj != null) {
					list.add(obj);
				}
			}
			this.baseDao.deleteAll(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void saveOrderLog(String orderid, String ip, String user,
			String lolOtype) {
		LogOrderLog orderlog = new LogOrderLog();
		orderlog.setLolAddress(ip);
		orderlog.setLolOrder(orderid);
		String state[]= { "以确定","订单无效","以配送","已发货","以退货","无法联系","结单"};
		orderlog.setLolOtype(state[Integer.parseInt(lolOtype)]);
		orderlog.setLolTime(new Date());
		orderlog.setLolUser(user);
		baseDao.save(orderlog);
	}

	@Override
	public PageCenterStation<?> findOrderLogList(int pageIndex, int pageSize,
			String boiId) {
		try {
			List list = new ArrayList();

			String hql = SQL_FROM + "LogOrderLog" + SQL_WHERE + " 1=1 ";
			String countHQL = SQL_SELECT + " count(*) " + SQL_FROM
					+ "LogOrderLog" + SQL_WHERE + " 1=1 ";

			if (boiId != null && boiId.length() > 0
					&& !boiId.equals("undefined") && !boiId.equals("null")) {
				hql += SQL_AND + " lolOrder =?";
				countHQL += SQL_AND + " lolOrder =?";
				list.add(boiId);
			}
			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao
					.queryList(countHQL, hql, list.toArray(), pageIndex * pageSize, pageSize);
			return p;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
